package sketch;


import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.imageio.*;
import java.io.File;
import java.io.IOException;

/**
 * @author Indrojeet Mondal
 * @version 1.0
 *
 */

public class JGraphSketch extends JFrame
{
	private GraphView graphView = new GraphView();
	private GraphController controller = new GraphController(graphView);
	
	private HelpDialog helpDialog;
	private JFileChooser chooser = new JFileChooser();
	
	public JGraphSketch()
	{
		super(appName);
		setSize(800, 600);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		try {
			URL url = getClass().getResource("icon.png");
			Image icon = ImageIO.read(url);
			this.setIconImage(icon);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		// do not show all files in chooser
		chooser.setAcceptAllFileFilterUsed(false);
		
		createMenus();
		
		add(controller, BorderLayout.WEST);
		add(graphView, BorderLayout.CENTER);
		/*
		try {
			String lf = UIManager.getSystemLookAndFeelClassName();
			UIManager.setLookAndFeel(lf);
			SwingUtilities.updateComponentTreeUI(this);
		}
		catch (Exception e) {
			e.printStackTrace();
		}*/
	}
	
	private void createMenus()
	{
		JMenu fileMenu = new JMenu("File");
		fileMenu.setMnemonic('F');
		
		JMenuItem exportItem = new JMenuItem("Export");
		exportItem.setToolTipText("Export to PNG file");
		exportItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				chooser.setFileFilter(new PNGFilter());
				int ret = chooser.showSaveDialog(JGraphSketch.this);
				if (ret == JFileChooser.APPROVE_OPTION)
					try {
						String fname = chooser.getSelectedFile().getAbsolutePath()+".png";
						graphView.export(new File(fname));
					} catch (IOException ie) {
						
						JOptionPane.showMessageDialog(JGraphSketch.this, ie.getMessage(),
								appName, JOptionPane.ERROR_MESSAGE);
					}
			}
		});
		fileMenu.add(exportItem);
		fileMenu.addSeparator();
		JMenuItem exitItem = new JMenuItem("Exit");
		exitItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
		});
		fileMenu.add(exitItem);
		
		JMenu helpMenu = new JMenu("Help");
		helpMenu.setMnemonic('H');
		
		JMenuItem helpItem = new JMenuItem("Help");
		helpItem.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					if (helpDialog == null) {
						helpDialog = new HelpDialog(JGraphSketch.this);
						
					}
					helpDialog.setVisible(true);
				}
			});
		helpMenu.add(helpItem);
		
		JMenuItem aboutItem = new JMenuItem("About");
		aboutItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				AboutDialog dlg = new AboutDialog(JGraphSketch.this);
				dlg.setVisible(true);
			}
		});
		helpMenu.add(aboutItem);
		
		JMenuBar menuBar = new JMenuBar();
		menuBar.add(fileMenu);
		menuBar.add(helpMenu);
		
		setJMenuBar(menuBar);
	}
	
	public static void main(String[] args) {
		JGraphSketch sketch = new JGraphSketch();
		sketch.setVisible(true);
	}
	public static final String appName = "JGraphSketch 1.0";
}

class PNGFilter extends FileFilter
{
	public boolean accept(File file) 
	{
		return file.isDirectory() | file.getName().endsWith("png") | file.getName().endsWith("PNG");
	}
	public String getDescription() 
	{
		return "PNG Image";
	}
	
}

